<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="股票代码" prop="stockCode">
        <el-input
          v-model="queryParams.stockCode"
          placeholder="请输入股票代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="策略分组" prop="strategyGroupId" label-width="auto">
        <el-select v-model="queryParams.strategyGroupId" placeholder="请选择策略分组" clearable>
          <el-option
            v-for="dict in strategyGroupList"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>


      <el-form-item label="策略名称" prop="strategyName">
        <el-input
          v-model="queryParams.strategyName"
          placeholder="请输入策略名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--          </el-form-item>-->
      <!--          <el-form-item label="动态止损率" prop="dynamicStopLossRate">-->
      <!--            <el-input-->
      <!--              v-model="queryParams.dynamicStopLossRate"-->
      <!--              placeholder="请输入动态止损率"-->
      <!--              clearable-->
      <!--              @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--          <el-form-item label="静态止盈率" prop="staticStopProfitRate">-->
      <!--            <el-input-->
      <!--              v-model="queryParams.staticStopProfitRate"-->
      <!--              placeholder="请输入静态止盈率"-->
      <!--              clearable-->
      <!--              @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--          <el-form-item label="静态止损率" prop="staticStopLossRate">-->
      <!--            <el-input-->
      <!--              v-model="queryParams.staticStopLossRate"-->
      <!--              placeholder="请输入静态止损率"-->
      <!--              clearable-->
      <!--              @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--          <el-form-item label="日交易次数限额" prop="tradeTimesLimit">-->
      <!--            <el-input-->
      <!--              v-model="queryParams.tradeTimesLimit"-->
      <!--              placeholder="请输入日交易次数限额"-->
      <!--              clearable-->
      <!--              @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--          <el-form-item label="日交易金额限额" prop="tradeAmountLimit">-->
      <!--            <el-input-->
      <!--              v-model="queryParams.tradeAmountLimit"-->
      <!--              placeholder="请输入日交易金额限额"-->
      <!--              clearable-->
      <!--              @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:config:add']"
        >新增
        </el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['business:config:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          icon="el-icon-delete"-->
      <!--          size="mini"-->
      <!--          :disabled="multiple"-->
      <!--          @click="handleDelete"-->
      <!--          v-hasPermi="['business:config:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['business:config:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="账户名称" align="center" prop="clientName"/>
      <el-table-column label="账户密码" align="center" prop="password"/>

      <el-table-column label="策略分组名称" align="center" prop="strategyGroupId" :formatter="strategyGroupFormat"/>
      <el-table-column label="策略ID" align="center" prop="id"/>
      <el-table-column label="策略名称" align="center" prop="strategyName"/>
      <el-table-column label="股票代码" align="center" prop="stockCode"/>
      <el-table-column label="动态止盈率(%)" align="center" prop="dynamicStopProfitRate"/>
      <!--      <el-table-column label="动态止损率" align="center" prop="dynamicStopLossRate" />-->
      <el-table-column label="静态止盈率(%)" align="center" prop="staticStopProfitRate"/>
      <el-table-column label="静态止损率(%)" align="center" prop="staticStopLossRate"/>
      <el-table-column label="日开仓次数限额" align="center" prop="tradeTimesLimit"/>
      <el-table-column label="日开仓金额限额" align="center" prop="tradeAmountLimit"/>
      <el-table-column label="单次开仓金额限额" align="center" prop="tradeAmountPerLimit"/>
      <el-table-column label="单笔开仓股数限额" align="center" prop="tradeVolumePerLimit"/>
      <el-table-column label="是否开启对冲交易" align="center" prop="tradeHedge">
        <template slot-scope="scope">
          <span v-if="scope.row.tradeHedge == false">关闭</span>
          <span v-if="scope.row.tradeHedge == true">开启</span>
        </template>
      </el-table-column>
      <el-table-column label="开仓策略" align="center" prop="openPositionStrategy">
        <template slot-scope="scope">
          <span v-if="scope.row.openPositionStrategy == 1">延迟开仓</span>
          <span v-if="scope.row.openPositionStrategy == 2">百分比开仓</span>
          <span v-if="scope.row.openPositionStrategy == 3">信号量开仓</span>
        </template>
      </el-table-column>

      <!--      <el-table-column label="首次开仓方向" align="center" prop="firstPositionType">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span v-if="scope.row.firstPositionType == 0">做多</span>-->
      <!--          <span v-if="scope.row.firstPositionType == 1">做空</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0">启用</span>
          <span v-if="scope.row.status == 1">禁用</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:config:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:config:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改交易参数设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" size="mini">

        <el-form-item label="策略分组" prop="strategyGroupId" label-width="auto">
          <el-select v-model="form.strategyGroupId" placeholder="请选择策略分组" clearable style="width: 460px;">
            <el-option
              v-for="dict in strategyGroupList"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="账户名称" prop="clientName" label-width="auto">
          <el-input v-model="form.clientName" placeholder="请输入账户名称" :disabled="editStatus"/>
        </el-form-item>
        <el-form-item label="账户密码" prop="password" label-width="auto">
          <el-input v-model="form.password" placeholder="请输入账户密码" :disabled="editStatus"/>
        </el-form-item>

        <el-form-item label="策略名称" prop="strategyName" label-width="auto">
          <el-input v-model="form.strategyName" placeholder="请输入策略名称" :disabled="editStatus"/>
        </el-form-item>
        <el-form-item label="股票代码" prop="stockCode" label-width="auto">
          <el-input v-model="form.stockCode" placeholder="请输入股票代码" :disabled="editStatus"/>
        </el-form-item>
        <el-form-item label="动态止盈率(%)" prop="dynamicStopProfitRate" label-width="auto">
          <el-input v-model="form.dynamicStopProfitRate" placeholder="请输入动态止盈率" :disabled="editStatus"/>
        </el-form-item>
        <!--        <el-form-item label="动态止损率" prop="dynamicStopLossRate">-->
        <!--          <el-input v-model="form.dynamicStopLossRate" placeholder="请输入动态止损率" />-->
        <!--        </el-form-item>-->
        <el-form-item label="静态止盈率(%)" prop="staticStopProfitRate" label-width="auto">
          <el-input v-model="form.staticStopProfitRate" placeholder="请输入静态止盈率"/>
        </el-form-item>
        <el-form-item label="静态止损率(%)" prop="staticStopLossRate" label-width="auto">
          <el-input v-model="form.staticStopLossRate" placeholder="请输入静态止损率" :disabled="editStatus"/>
        </el-form-item>
        <el-form-item label="日开仓次数限额" prop="tradeTimesLimit" label-width="auto">
          <el-input v-model="form.tradeTimesLimit" placeholder="请输入日开仓次数限额"/>
        </el-form-item>
        <el-form-item label="日开仓金额限额" prop="tradeAmountLimit" label-width="auto">
          <el-input v-model="form.tradeAmountLimit" placeholder="请输入日开仓金额限额"/>
        </el-form-item>
        <el-form-item label="单次开仓金额限额" prop="tradeAmountPerLimit" label-width="auto">
          <el-input v-model="form.tradeAmountPerLimit" placeholder="请输入单次开仓金额限额"/>
        </el-form-item>
        <el-form-item label="单笔开仓股数限额" prop="tradeVolumePerLimit" label-width="auto">
          <el-input v-model="form.tradeVolumePerLimit" placeholder="请输入单笔开仓股数限额"/>
        </el-form-item>

        <el-form-item label="开仓策略" prop="openPositionStrategy" label-width="auto">
          <el-radio-group v-model="form.openPositionStrategy" size="medium">
            <el-radio border :label="1">延迟开仓</el-radio>
            <el-radio border :label="2">百分比开仓</el-radio>
            <el-radio border :label="3">信号量开仓</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="首次开仓延迟分钟" prop="positionDelayMinute" label-width="auto"
                      v-show="form.openPositionStrategy==1">
          <el-input v-model="form.positionDelayMinute" placeholder="请输入首次开仓延迟分钟"/>
        </el-form-item>

        <el-form-item label="首次交易方向" prop="firstPositionType" label-width="auto" v-show="form.openPositionStrategy==1">
          <el-radio-group v-model="form.firstPositionType" size="medium">
            <el-radio border :label="0">做多</el-radio>
            <el-radio border :label="1">做空</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="开盘价上涨(下跌)百分比%" prop="openPositionRate" label-width="auto"
                      v-show="form.openPositionStrategy==2">
          <el-input v-model="form.openPositionRate" placeholder="请输入上涨(下跌)百分比"/>
        </el-form-item>
        <el-form-item label="跌幅（%）" prop="dropExtentRate" label-width="auto" v-show="form.openPositionStrategy==3">
          <el-input v-model="form.openPositionSemaphore.dropExtentRate" placeholder="请输入跌幅（%）值"/>
        </el-form-item>


        <el-form-item label="首次对冲交易" prop="tradeHedge" label-width="auto">
          <el-radio-group v-model="form.tradeHedge" size="medium">
            <el-radio border :label="false">关闭</el-radio>
            <el-radio border :label="true">开启</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-row v-show="form.tradeHedge">
          <el-col :span="16">
            <el-form-item label="做多开仓金额" prop="tradeHedgeSaleAmount" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeSaleAmount"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="做空开仓金额" prop="tradeHedgeSellAmount" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeSellAmount"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-show="form.tradeHedge">
          <el-col :span="16">
            <el-form-item label="主单开仓金额" prop="tradeHedgeMainAmount" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeMainAmount" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="对冲单开仓金额" prop="tradeHedgeAmount" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeAmount"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-show="form.tradeHedge">
          <el-col :span="16">
            <el-form-item label="做多动态止盈率(%)" prop="tradeHedgeSaleDynamicProfitRate" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeSaleDynamicProfitRate" :min="0.1"
                               :max="1"></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="做空动态止盈率(%)" prop="tradeHedgeSellDynamicProfitRate" label-width="auto">
              <el-input-number size="small" v-model="form.tradeHedgeSellDynamicProfitRate" :min="0.1"
                               :max="1"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>


        <el-form-item label="策略状态" prop="status" label-width="auto">
          <el-radio-group v-model="form.status" size="medium">
            <el-radio border :label="0">启用</el-radio>
            <el-radio border :label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listConfig, getConfig, delConfig, addConfig, updateConfig} from "@/api/business/config";
import {getData, getDicts as getDicts} from '@/api/system/dict/data'
import { parseStrEmpty } from "@/utils/ruoyi";

export default {
  name: "Config",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 交易参数设置表格数据
      configList: [],

      //分组类别数据
      strategyGroupList: [],


      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      editStatus: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockCode: null,
        strategyGroupId: null,
        strategyName: null,
        dynamicStopProfitRate: null,
        dynamicStopLossRate: null,
        staticStopProfitRate: null,
        staticStopLossRate: null,
        tradeTimesLimit: null,
        tradeAmountLimit: null,
        tradeAmountPerLimit: null,
        tradeVolumePerLimit: null,
        positionDelayMinute: null,
        openPositionRate: null,
        openPositionDealAmount: null,
        openPositionSemaphore:{
          dropExtentRate: null
        },
        tradeHedge: null,
        tradeHedgeSaleAmount: null,
        tradeHedgeSellAmount: null,
        tradeHedgeMainAmount: null,
        tradeHedgeAmount: null,
        tradeHedgeSaleDynamicProfitRate: null,
        tradeHedgeSellDynamicProfitRate: null,
        openPositionStrategy: null,
        status: null,
        firstPositionType: null
      },
      // 表单参数
      form: {
        openPositionSemaphore: {
          dropExtentRate: null
        }
      },
      // 表单校验
      rules: {
        stockCode: [
          {required: true, message: '股票代码不能为空', trigger: 'blur'}
        ],
        strategyGroupId: [
          {required: true, message: '分组策略不能为空', trigger: 'blur'}
        ],
        // dynamicStopProfitRate: [
        //   {required: true, message: '动态止盈率不能为空', trigger: 'blur'}
        // ],
        staticStopLossRate: [
          {required: true, message: '静态止损率不能为空', trigger: 'blur'}
        ],
        tradeTimesLimit: [
          {required: true, message: '开仓次数不能为空', trigger: 'blur'}
        ],
        tradeAmountLimit: [
          {required: true, message: '日开仓金额不能为空', trigger: 'blur'}
        ],
        // tradeAmountPerLimit: [
        //   {required: true, message: '单次开仓金额不能为空', trigger: 'blur'}
        // ],
        tradeVolumePerLimit: [
          {required: true, message: '单笔开仓股数不能为空', trigger: 'blur'}
        ]

      }

    };
  },
  created() {
    this.getStrategyGroupList();
    this.getList();

  },
  methods: {
    /** 查询交易参数设置列表 */
    getList() {
      this.loading = true;
      listConfig(this.queryParams).then(response => {
        this.configList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询策略组别列表 */
    async getStrategyGroupList() {
      await getDicts("business_strategy_group").then(response => {
        console.log("strategyGroupList:", response)
        this.strategyGroupList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockCode: null,
        dynamicStopProfitRate: null,
        dynamicStopLossRate: null,
        staticStopProfitRate: null,
        staticStopLossRate: null,
        tradeTimesLimit: null,
        tradeAmountLimit: null,
        tradeHedge: false,
        firstPositionType: null,
        status: null,
        createTime: null,
        createBy: null,
        openPositionSemaphore:{
          dropExtentRate:null
        }

      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    strategyGroupFormat(row) {
      return this.selectDictLabel(this.strategyGroupList, row.strategyGroupId);
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加交易参数设置";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfig(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.editStatus = false;
        this.title = "修改交易参数设置";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfig(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfig(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除交易参数设置编号为"' + ids + '"的数据项？').then(function () {
        return delConfig(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
